package com.example.mall.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.example.mall.common.CommonResult;
import com.example.mall.entity.PmsBrand;
import com.example.mall.service.MyRedisService;
import com.example.mall.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Set;

/*
* Redis测试Controller
* */
@Api(tags = "ReidsController",description = "Redis测试")
@Controller
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private MyRedisService redisService;
    @Autowired
    private PmsBrandService brandService;
    @ApiOperation("测试简单缓存")
    @RequestMapping(value = "/simpleTest",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsBrand> simpleTest(){
        List<PmsBrand> brandList = brandService.listBrand(1,5);
        PmsBrand brand = brandList.get(0);
        String key = "redis:simple:"+brand.getId();
        redisService.set(key,brand);
        PmsBrand cacheBrand = (PmsBrand) redisService.get(key);
        return CommonResult.success(cacheBrand);
    }

    @ApiOperation("测试Hash结构的缓存")
    @RequestMapping(value = "/hashTest",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsBrand> hashTest(){
        List<PmsBrand> brandList = brandService.listBrand(1, 5);
        PmsBrand brand = brandList.get(0);
        String key = "redis:hash:"+brand.getId();
        Map<String,Object> value = BeanUtil.beanToMap(brand);
        redisService.hSetAll(key,value);
        Map<Object,Object> cacheValue = redisService.hGetAll(key);
        PmsBrand cacheBrand = BeanUtil.mapToBean(cacheValue,PmsBrand.class,true);
        return CommonResult.success(cacheBrand);
    }

    @ApiOperation("测试Set结构的缓存")
    @RequestMapping(value = "/setTest",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Set<Object>> setTest(){
        List<PmsBrand> brandList = brandService.listBrand(1,5);
        String key = "redis:set:all";
        redisService.sAdd(key,(Object[]) ArrayUtil.toArray(brandList,PmsBrand.class));
        redisService.sRemove(key,brandList.get(0));
        Set<Object> cacheBrandList = redisService.sMembers(key);
        return CommonResult.success(cacheBrandList);
    }

    @ApiOperation("测试List结构的缓存")
    @RequestMapping(value = "/listTest",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Object>> listTest(){
        List<PmsBrand> brandList = brandService.listBrand(1,5);
        String key = "redis:list:all";
        redisService.lPushAll(key,(Object[])ArrayUtil.toArray(brandList,PmsBrand.class));
        redisService.lRemove(key,1,brandList.get(0));
        List<Object> cacheBrandList = redisService.lRange(key,0,3);
        return CommonResult.success(cacheBrandList);
    }
}
